home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / EXECCMD.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  6KB  |  229 lines

  1. #include "pt.h"
  2. #include "stdlib.h"
  3. #include "fcntl.h"
  4. #include "process.h"
  5. #include "malloc.h"
  6. #include "direct.h"
  7. #include "string.h"
  8. #include "conio.h"
  9. #include "io.h"
  10.  
  11. int pascal
  12. /* XTAG:makeName */
  13. makeName(s)
  14.     register unsigned char *s;
  15. {
  16.     register int l;
  17.     int nTries;
  18.     unsigned char ch;
  19.  
  20.     /* try to create a new name by changing the last letter */
  21.     l = strlen(s) - 1;
  22.     nTries = 0;
  23.     ch = tolower(s[l]);    /* save first character */
  24.     while( access(s, 0) == 0 ) {
  25.         /* the file name exists so try another */
  26.         ++nTries;
  27.         if( s[l] == 'z' )
  28.             s[l] = 'a';
  29.         else
  30.             ++s[l];
  31.         if( s[l] == ch )
  32.             return -1;    /* could not find an unused name */
  33.     }
  34.     return nTries;
  35. }
  36.  
  37. void pascal
  38. /* XTAG:execCmd */
  39. execCmd(cmdType)
  40.     int cmdType;
  41. {
  42.     extern unsigned char msgBuffer[];
  43.     extern unsigned char textBuffer[];
  44.     extern int menuRow, menuCol;
  45.     extern struct window *selWindow;
  46.     extern long selBegin, selEnd;
  47.     extern union REGS rin, rout;
  48.     extern struct SREGS segRegs;
  49.     extern unsigned int dispMemory;
  50.     extern int scrRows, scrCols;
  51.     extern int menuLine;
  52.     extern unsigned char *userMessages[];
  53.     extern int debug;
  54.     extern unsigned char currentDirectory[];
  55.     extern unsigned char currentDrive;
  56.     extern unsigned char startDirectory[];
  57.     extern unsigned char startDrive;
  58.  
  59.     int fid, saveOut, saveErr, outHandle;
  60.     int row1, row2, col1, col2;
  61.     int mouseStateSize;
  62.     char *mouseState;
  63.     struct SREGS newRegs;
  64.     long cp;
  65.     unsigned char outFile[13], cmdLine[150], cmdName[64], *p, ch;
  66.     unsigned char *dosExit;
  67.  
  68.     /* use the current directory that the user set last */
  69.     setDefaultDrive(currentDrive);
  70.     chdir(currentDirectory);
  71.  
  72. if( cmdType == FEXECSHELL ) {    /* interactive */
  73.     fid = getDOScolor();
  74.     setMap(0, 0, scrRows-1, scrCols-1, 0, fid);
  75.     updateScreen(0, scrRows-1);
  76.     setCPos(0, 0);
  77.     cprintf("EXIT returns you to Point\r\n");
  78. } else {
  79.     if( cmdType == FEXECCMD ) {
  80.         /* get the window corners from the user */
  81.         if( getBox(0, 0, &row1, &col1, &row2, &col2, 0) == 2 )
  82.             return;
  83.         if( row1 == row2 && col1 == col2 ) {
  84.             row2 = scrRows-1;
  85.             if( menuLine < 0 )
  86.                 --row2;
  87.             col2 = scrCols-1;
  88.         }
  89.     }
  90.  
  91.     /* generate names for the  output temp file */
  92.     strcpy(outFile, "UnNamed.a");
  93.     makeName(outFile);
  94.  
  95.     /* handle redirection of the output */
  96.     saveOut = dup(1);
  97.     saveErr = dup(2);
  98.     outHandle = creatls(outFile, 0);
  99.     if( outHandle == -4 ) {
  100.         msg(userMessages[HANDLEMSG], 3);
  101.         closels(saveOut);
  102.         return;
  103.     } else if( outHandle < 0 ) {
  104.         sprintf(msgBuffer,
  105.             "Cannot create file [%s] ret=%d", outFile, outHandle);
  106.         msg(msgBuffer, 2);
  107.         closels(saveOut);
  108.         return;
  109.     } else {
  110.         dup2(outHandle, 1);
  111.         dup2(outHandle, 2);
  112.         closels(outHandle);
  113.     }
  114. }
  115.  
  116.     /* save the mouse state */
  117.     rin.x.ax = 21;
  118.     int86(51, &rin, &rout);
  119.     mouseStateSize = rout.x.bx;
  120.     mouseState = (char *)malloc(mouseStateSize);
  121.     if( mouseState != NULL) {
  122.         rin.x.ax = 22;        /* save mouse state */
  123.         rin.x.dx = (unsigned int)mouseState;
  124.         newRegs.es = segRegs.ds;    /* put DS into ES */
  125.         newRegs.cs = segRegs.cs;    /* other segment regs */
  126.         newRegs.ss = segRegs.ss;    /* are unchanged */
  127.         newRegs.ds = segRegs.ds;
  128.         int86x(51, &rin, &rout, &newRegs);
  129.         rin.x.ax = 2;    /* hide the mouse cursor */
  130.         int86(51, &rin, &rout);
  131.         /* reset the cursor to the usual shape */
  132.         if( dispMemory == 0xB000 )
  133.             setCType(12, 13);
  134.         else
  135.             setCType(6, 7);
  136.     }
  137.  
  138.     /* execute the command */
  139.     if( cmdType == FEXECSHELL ) {
  140.         dosExit = getenv("COMSPEC");
  141.         spawnl(P_WAIT, dosExit, dosExit, NULL);
  142.     } else {
  143.         strcpy(&cmdLine[0], " /C ");
  144.         p = cmdLine + 4;
  145.         cp = selBegin;
  146.         fid = selWindow->fileId;
  147.         while( cp <= selEnd )
  148.             *p++ = readChar(fid, cp++);
  149.         /* look for CRs, LFs, tabs, and blanks at end of the */
  150.         /* command and delete them */
  151.         while( 1 ) {
  152.             ch = *(p-1);
  153.             if( ch=='\n' || ch=='\r' || ch=='\t' || ch==' ' )
  154.                 --p;
  155.             else
  156.                 break;
  157.         }
  158.         *p = '\0';
  159.         strcpy(cmdName, getenv("COMSPEC"));
  160.         sprintf(msgBuffer,
  161.             "Please wait while executing selection <%s>",
  162.             /* skip the " /C " at the beginning of cmdLine */
  163.             &cmdLine[4]);
  164.         msg(msgBuffer, 1);
  165.         spawnl(P_WAIT, cmdName, cmdName, cmdLine, NULL);
  166.     }
  167.  
  168.     /* reinitialize the mouse in case the program we called used it */
  169.     if( mouseState == NULL ) {
  170.         initMouse(5, 10, 1);
  171.     } else {
  172.         rin.x.ax = 23;        /* restore mouse state */
  173.         rin.x.dx = (unsigned int)mouseState;
  174.         int86x(51, &rin, &rout, &newRegs);
  175.         rin.x.ax = 1;    /* show the mouse cursor */
  176.         int86(51, &rin, &rout);
  177.         /* restore the Point cursor shape */
  178.         if( dispMemory == 0xB000 )
  179.             setCType(4, 9);
  180.         else
  181.             setCType(2, 5);
  182.         free(mouseState);
  183.     }
  184.  
  185.     switch( cmdType ) {
  186.  
  187.     case FEXECSHELL:
  188.     redraw:
  189.         redrawBox(0, 0, scrRows-1, scrCols-1);
  190.         updateScreen(0, scrRows-1);
  191.         break;
  192.  
  193.     case FEXECCMD:
  194.         /* undo the redirection */
  195.         dup2(saveOut, 1);
  196.         dup2(saveErr, 2);
  197.         closels(saveOut);
  198.         /* show the output file */
  199.         createWindow(outFile, row1, col1, row2, col2, CRTOP, 0);
  200.         updateScreen(0, scrRows-1);
  201.         break;
  202.  
  203.     case FREPLACECMD:
  204.         fid = selWindow->fileId;
  205.         deleteChars(fid, 0, 1);
  206.         outHandle = openls(outFile, 0);
  207.         while( 1 ) {
  208.             saveOut = readls(outHandle, &textBuffer[0],
  209.                 MSGBUFFERSIZE);
  210.             if( saveOut <= 0 )
  211.                 break;
  212.             for( row1 = 0; row1 < saveOut; ++row1)
  213.                 insertChar(textBuffer[row1]);
  214.         }
  215.         closels(outHandle);
  216.         unlink(outFile);
  217.         goto redraw;
  218.     }
  219.  
  220.     /* remember the user's current directory and drive */
  221.     (void)getcwd(¤tDirectory[0], FILENAMESIZE);
  222.     currentDrive = getDefaultDrive();
  223.  
  224.     /* and return to the startup drive and directory */
  225.     setDefaultDrive(startDrive);
  226.     chdir(startDirectory);
  227.  
  228. }
  229.